#!/bin/sh
#

# Exit on error
set -e

# Initialize the $iraf and environment.
if [ -z "$iraf" ]; then
  if [ -e "$HOME/.iraf/setup.sh" ]; then
    . "$HOME/.iraf/setup.sh"
  else
    . unix/hlib/setup.sh
  fi
else
    . "$iraf/unix/hlib/setup.sh"
fi


if [ -z "$iraf" ]; then
    iraf=$(pwd)/
fi

if [ -e "$HOME/.iraf/arch" ]; then
  if [ -n "$IRAFARCH" ]; then
    iarch=$(cat "$HOME/.iraf/arch")
    if [ "$iarch" != "$IRAFARCH" ]; then
      echo
      echo 'Error:  Your $IRAFARCH does not match $HOME/.iraf/arch,'
      echo '        Please change $IRAFARCH or rerun install script.'
      echo
      exit 1
    fi
  fi
fi

c_start=$(date)


"$iraf/util/mkclean"				# clean old binaries

. "$iraf/unix/hlib/irafuser.sh"			# NOVOS bootstrap
(cd "$iraf/unix" && sh -x mkpkg.sh)
(cd "$iraf/sys"	&& mkpkg)			# build NOVOS

. "$iraf/unix/hlib/irafuser.sh"			# VOS bootstrap
(cd "$iraf/unix/boot" && sh -x mkpkg.sh && mv -v "$hlib"/*.e "$hbin")
(cd "$iraf/vendor" && make all)			# build vendor libs
(cd "$iraf/" && mkpkg)				# build core system
(cd "$iraf/noao" && noao=$(pwd)/ mkpkg -p noao)	# build NOAO package

rm -rf bin*/pkgconfig			# misc clean up

c_end=$(date)


echo
echo
echo
echo "Start:  $c_start"
echo "  End:  $c_end"
